<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xiaobai
 * Date: 13-5-24
 * Time: 上午10:18
 * To change this template use File | Settings | File Templates.
 */
class Cookie_model extends CI_Model {
    const PLATFORM_YUNYUN = 'yunyun';
    private $_write_db;
    function __construct()
    {
        parent::__construct();
        $this->_write_db = $this->load->database('master', TRUE);
    }

    public function get_cookie($platform, $need_delay = true) {
    	$time_now = time();
    	
    	$this->db->select('*');
    	$this->db->from('weibo_accounts');
    	$this->db->where('platform', $platform);
    	$this->db->where('status', 1);
        if($need_delay){
            $this->db->where('last_login <', ($time_now - 5));
        }
    	$this->db->order_by('last_login');
    
    	$query = $this->db->get();
    	if($query->num_rows() <= 0) {
    		return $this->error_code_library->getError('system', 'data', 'no_data', 'In Function '. __FUNCTION__);
    	} else {
    		$result = $query->row_array();
    		
    		$this->_write_db->set('last_login', $time_now);
    		$this->_write_db->where('`id` = '.$result['id']);
    		$this->_write_db->update('weibo_accounts');
    		
    		return $result;
    	}
    }


    /**
     * 获取某个平台下可以用的cookie总量
     * @param $platform 平台
     * @return string 可用值
     */
    public function get_cookie_count($platform) {
        $this->db->where('platform', $platform);
        $this->db->where('status', 1);
        $this->db->from('weibo_accounts');
        $count = $this->db->count_all_results();
        return $count;
    }

    public function get_cookie_bak($platform) {
        $this->db->select('*');
        $this->db->from('weibo_accounts');
        $this->db->where('platform', $platform);
        $this->db->where('status', 1);
        if(($platform == self::PLATFORM_YUNYUN) || ($platform == 'sina')) {
            $this->db->order_by('wap_count');
        } else {
            $this->db->order_by('use_count');
        }

        $this->db->limit(20);
        $query = $this->db->get();
        if($query->num_rows() <= 0) {
            return $this->error_code_library->getError('system', 'data', 'no_data', 'In Function '. __FUNCTION__);
        } else {
            $result_array = $query->result_array();
            $rand_key = array_rand($result_array);
            return $result_array[$rand_key];
        }
    }
    
    public function get_unlock_cookie($platform) {
		
    	
    	$this->db->select('*');
    	$this->db->from('weibo_accounts');
    	$this->db->where('platform', $platform);
    	$this->db->where('status', 0);
    	
    	$this->db->order_by('last_login', "DESC");
    
    	$query = $this->db->get();
    	if($query->num_rows() <= 0) {
    		return $this->error_code_library->getError('system', 'data', 'no_data', 'In Function '. __FUNCTION__);
    	} else {
    		$result = $query->row_array();
    		
    		return $result;
    	}
    }

    public function update_cookie_use_count($id = NULL) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', '$id In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('use_count', 'use_count+1', FALSE);
            $this->_write_db->where('`id` = '.$id);
            $result = $this->_write_db->update('weibo_accounts');
        }

    }

    public function update_cookie_wap_count($id = NULL) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', '$id In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('wap_count', 'wap_count+1', FALSE);
            $this->_write_db->where('`id` = '.$id);
            $result = $this->_write_db->update('weibo_accounts');
        }
    }

    public function label_cookie($id = NULL) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', '$id In Function '. __FUNCTION__);
        } else {
            $this->_write_db->set('status', 0);
            $this->_write_db->where('`id` = '.$id);
            $result = $this->_write_db->update('weibo_accounts');
        }
    }
    
    public function unlock_cookie($id = NULL) {
    	if(is_null($id)) {
    		return $this->error_code_library->getError('system', 'params', 'no_must_parameter', '$id In Function '. __FUNCTION__);
    	} else {
    		$this->_write_db->set('status', 1);
    		$this->_write_db->where('`id` = '.$id);
    		$result = $this->_write_db->update('weibo_accounts');
            echo $this->_write_db->last_query();
            var_dump($result);
    	}
    }
}
